package com.example.myproject.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myproject.Sqlite.MySqliteOpenHelper;
import com.example.myproject.R;
import com.example.myproject.pojo.User;

public class Register_activity extends AppCompatActivity implements View.OnClickListener{
    private Button register;
    private EditText username,password;
    private MySqliteOpenHelper mySqliteOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mySqliteOpenHelper = new MySqliteOpenHelper(this, "users", null, 1);
        find();
    }

    private void find() {
        register=findViewById(R.id.register);
        username=findViewById(R.id.register_username);
        password=findViewById(R.id.register_password);
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        User user = new User();
        String username1=username.getText().toString();
        String password1=password.getText().toString();
        if(username1.isEmpty()||username1.length()<=0){
            Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            return;
        }
        if(password1.isEmpty()||password1.length()<=0){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
        }
        user.setUsername(username.getText().toString());
        user.setPassword(password.getText().toString());
        long register = mySqliteOpenHelper.register(user,this);
        if(register>0){
            Toast.makeText(this,"注册成功!",Toast.LENGTH_LONG).show();
        }
    }
}
